﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Zatify.Domain.Data;

namespace Zatify.WebUI.HtmlHelpers
{
    public static class CategoryBreadCrumbsHelper
    {
        public static MvcHtmlString CategoryBreadCrumbs(this HtmlHelper html, Category category, Func<int, string> func)
        {
            var result = new StringBuilder();

            if (category.ParentId > 0)
                result.Append(CategoryBreadCrumbs(html, category.ParentCategory, func).ToString());

            var tagAnchor = new TagBuilder("a");
            tagAnchor.MergeAttribute("href", func(category.CategoryId));

            var tagBtn = new TagBuilder("input");
            tagBtn.MergeAttribute("type", "button");
            tagBtn.MergeAttribute("class", "btn apps_btn apps_gray_btn");
            tagBtn.MergeAttribute("value", category.CategoryName);

            tagAnchor.InnerHtml = tagBtn.ToString();

            result.Append(tagAnchor.ToString());

            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString CategoryBreadCrumbsSeparated(this HtmlHelper html, Category category, Func<int, string> func, string delimeter = " \\ ")
        {
            var result = new StringBuilder();

            if (category.ParentId > 0)
                result.Append(CategoryBreadCrumbsSeparated(html, category.ParentCategory, func, delimeter).ToString());

            if (category.ParentId.HasValue)
            {
                var tagSpanDelimeter = new TagBuilder("span");
                tagSpanDelimeter.InnerHtml = delimeter;
                tagSpanDelimeter.MergeAttribute("class", "delimeter");

                result.Append(tagSpanDelimeter.ToString());
            }

            var tagSpan = new TagBuilder("span");
            tagSpan.InnerHtml = category.CategoryName;
            result.Append(tagSpan.ToString());

            return MvcHtmlString.Create(result.ToString());
        }


        public static MvcHtmlString CategoryBreadCrumbsAsButtons(this HtmlHelper html, Category category, Func<int, string> func)
        {
            var result = new StringBuilder();

            if (category.ParentId > 0)
                result.Append(CategoryBreadCrumbsAsButtons(html, category.ParentCategory, func).ToString());

            var divTag = new TagBuilder("div");
            divTag.AddCssClass("categories_line categories_inline category_navbar");
            divTag.InnerHtml = category.CategoryName;

            var tagAnchor = new TagBuilder("a");

            if (category.ChildCategories.Count() > 0)
                tagAnchor.MergeAttribute("href", func(category.CategoryId));
            else
                tagAnchor = new TagBuilder("span");

            tagAnchor.SetInnerText(category.CategoryName);

            divTag.InnerHtml = tagAnchor.ToString();

            if (category.IsSelectable())
            {
                var tagInputChk = new TagBuilder("input");
                tagInputChk.AddCssClass("chkCategory_" + category.CategoryId);
                tagInputChk.MergeAttribute("type", "checkbox");
                tagInputChk.MergeAttribute("onchange", "addCategory(this, " + category.CategoryId + ")");

                tagInputChk.AddCssClass("promotion_checkbox");

                divTag.InnerHtml += tagInputChk.ToString();
            }

            result.Append(divTag.ToString());

            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString CategoryBreadCrumbsAsButtons(this HtmlHelper html, Category category, List<Category> selectedCategories, Func<int, string> func)
        {
            var result = new StringBuilder();

            if (category.ParentId > 0)
                result.Append(CategoryBreadCrumbsAsButtons(html, category.ParentCategory, selectedCategories, func).ToString());

            var divTag = new TagBuilder("div");
            divTag.AddCssClass("categories_line categories_inline category_navbar");
            divTag.InnerHtml = category.CategoryName;

            var tagAnchor = new TagBuilder("a");

            if (category.ChildCategories.Count() > 0)
                tagAnchor.MergeAttribute("href", func(category.CategoryId));
            else
                tagAnchor = new TagBuilder("span");

            tagAnchor.SetInnerText(category.CategoryName);

            divTag.InnerHtml = tagAnchor.ToString();

            if (category.IsSelectable())
            {
                var tagInputChk = new TagBuilder("input");
                tagInputChk.AddCssClass("chkCategory_" + category.CategoryId);
                tagInputChk.MergeAttribute("type", "checkbox");
                tagInputChk.MergeAttribute("onchange", "addCategory(this, " + category.CategoryId + ")");

                if (selectedCategories.Any(c => c.CategoryId == category.CategoryId))
                {
                    tagInputChk.MergeAttribute("checked", "checked");
                    tagInputChk.MergeAttribute("disabled", "disabled");
                }

                tagInputChk.AddCssClass("promotion_checkbox");

                divTag.InnerHtml += tagInputChk.ToString();
            }

            result.Append(divTag.ToString());

            return MvcHtmlString.Create(result.ToString());
        }
    }
}